package com.aricwang.easyprofile.database;

import com.aricwang.easyprofile.rule.Rule;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class RuleTable {
	// Database table
	public static final String TABLE_NAME = "rules";
	
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_PRIORITY = "priority";
	
	public static final String COLUMN_PROFILE = "profile";
	
	public static final String COLUMN_TIMEFRAME_START_HOUR = "timeframe_start_hour";
	public static final String COLUMN_TIMEFRAME_START_MINUTE = "timeframe_start_minute";
	public static final String COLUMN_TIMEFRAME_END_HOUR = "timeframe_end_hour";
	public static final String COLUMN_TIMEFRAME_END_MINUTE = "timeframe_end_minute";
	public static final String COLUMN_TIMEFRAME_REPEAT = "timeframe_repeat";
	
	public static final String COLUMN_CHARGING = "charging";
	public static final String COLUMN_PLACE = "place";
	
	public static final String[] ALL_COLUMNS = { 
		RuleTable.COLUMN_ID,
		RuleTable.COLUMN_NAME,
		RuleTable.COLUMN_PRIORITY,
		RuleTable.COLUMN_PROFILE,
		RuleTable.COLUMN_TIMEFRAME_START_HOUR,
		RuleTable.COLUMN_TIMEFRAME_START_MINUTE,
		RuleTable.COLUMN_TIMEFRAME_END_HOUR,
		RuleTable.COLUMN_TIMEFRAME_END_MINUTE,
		RuleTable.COLUMN_TIMEFRAME_REPEAT,
		RuleTable.COLUMN_CHARGING,
		RuleTable.COLUMN_PLACE
	};


	// Database creation SQL statement
	private static final String DATABASE_CREATE = "create table " 
			+ TABLE_NAME
			+ " (" 
			+ COLUMN_ID + " integer primary key autoincrement, " 
			+ COLUMN_NAME + " text not null, " 
			+ COLUMN_PRIORITY + " integer default 10, "
			+ COLUMN_PROFILE + " integer default 0, "
			+ COLUMN_TIMEFRAME_START_HOUR + " integer default 0, "
			+ COLUMN_TIMEFRAME_START_MINUTE + " integer default 0, "
			+ COLUMN_TIMEFRAME_END_HOUR + " integer default 0, "
			+ COLUMN_TIMEFRAME_END_MINUTE + " integer default 0, "
			+ COLUMN_TIMEFRAME_REPEAT + " integer default 0, "
			+ COLUMN_CHARGING + " integer default 0, "
			+ COLUMN_PLACE + " integer default -1 "
			+ ");";

	public static void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
		insertDefaults(database);
	}

	public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
		Log.w(ProfileTable.class.getName(), "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		
		onCreate(database);
	}
	
	private static void insertDefaults(SQLiteDatabase database) {
		// profile 'Default'
		insertRecord(database, "Sleeping", 50, 5, 23, 0, 8, 30, Rule.WEEK, Rule.DONT_CARE, Rule.NO_PLACE);
		insertRecord(database, "Charging", 40, 4, 0, 0, 0, 0, 0, Rule.CHARGING, Rule.NO_PLACE);
		insertRecord(database, "Home", 30, 3, 0, 0, 0, 0, 0, Rule.DONT_CARE, Rule.NO_PLACE);
		insertRecord(database, "Work", 20, 2, 0, 0, 0, 0, 0, Rule.DONT_CARE, Rule.NO_PLACE);
		insertRecord(database, "Default", 10, 1, 0, 0, 0, 0, 0, Rule.DONT_CARE, Rule.NO_PLACE);
	}
	
	private static void insertRecord(SQLiteDatabase database, String name, 
			int priority, int profile, int timeframeStartHour, int timeframeStartMinute,
			int timeframeEndHour, int timeframeEndMinute, int timeframeRepeat, 
			int charging, int place) 
	{
		String sql = "insert into " + TABLE_NAME  
				+ " ( "
				+ COLUMN_NAME + ", " 
				+ COLUMN_PRIORITY + ", "
				+ COLUMN_PROFILE + ", "
				+ COLUMN_TIMEFRAME_START_HOUR + ", "
				+ COLUMN_TIMEFRAME_START_MINUTE + ", "
				+ COLUMN_TIMEFRAME_END_HOUR + ", "
				+ COLUMN_TIMEFRAME_END_MINUTE + ", "
				+ COLUMN_TIMEFRAME_REPEAT + ", "
				+ COLUMN_CHARGING + ", "
				+ COLUMN_PLACE
				+ ") values ( '"
				+ name + "', "
				+ priority + ", "
				+ profile + ", "
				+ timeframeStartHour + ", "
				+ timeframeStartMinute + ", "
				+ timeframeEndHour + ", "
				+ timeframeEndMinute + ", "
				+ timeframeRepeat + ", "
				+ charging + ", "
				+ place 
				+ " );";
	
		database.execSQL(sql);
	}	
}
